<?php

namespace App\Http\Requests;


use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends  FormRequest
{

    /**
     * @var bool  验证遇到第一个错误就停止验证
     */
    protected $stopOnFirstFailure = true;

    /**
     * @param Validator $validated
     * @return void
     */
    public function failedValidation(Validator $validated)
    {
        $message = $validated->getMessageBag();
        validatedException($message->first());
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        $actionName = request()->route()->getActionMethod();
        $ruleActionName = $actionName.'Rules';
        if (method_exists($this,$ruleActionName)){
            return $this->$ruleActionName();
        }
        //没创建则返回空 不做校验
        return [];
    }
}